package tr.gov.tubitak.uekae.applet.resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tr.gov.tubitak.uekae.esya.api.certificate.validation.policy.PolicyReader;
import tr.gov.tubitak.uekae.esya.api.certificate.validation.policy.ValidationPolicy;
import tr.gov.tubitak.uekae.esya.api.common.ESYAException;
import tr.gov.tubitak.uekae.esya.api.signature.config.ConfigurationException;
import tr.gov.tubitak.uekae.esya.api.xmlsignature.config.Config;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class ResourceUtil 
{
    private static final Logger msLogger = LoggerFactory.getLogger(ResourceUtil.class.getName());

    public static String licensePath = "E:/lisans/lisans.xml";
    public static String policyPath = "E:/policy/certval-ug-policy.xml";
    public static String log4jPath = "E:/log4j/log4j.properties";

	public static InputStream getFile(String aFileName) throws IOException
	{
        msLogger.debug("getting file");
        try {
		    URL url = ResourceUtil.class.getResource(aFileName);
            msLogger.debug("url path " + url.getPath());

            msLogger.debug("opening stream");
            InputStream is = url.openStream();
            msLogger.debug("stream opened");
		    return is;
        }
        catch (Exception e) {
            msLogger.debug("Error in getting file " + aFileName);
            e.printStackTrace();
        }
        return null;
	}
	
	public static ValidationPolicy getPolicy() throws IOException, ESYAException {

        System.out.println("getting policy");
		InputStream policyStream = getFile("certval-policy-test.xml");

        System.out.println("validation policy is being created");
		ValidationPolicy policy = PolicyReader.readValidationPolicy(policyStream);
        System.out.println("validation policy is created, being returned");
		return policy;
	}

    public static Config getConfig() throws IOException {

        msLogger.debug("getting config");
        InputStream configStream = getFile("xmlsignature-config.xml");

        msLogger.debug("config is being created");

        Config config = new Config(configStream);

        URL configUrl = ResourceUtil.class.getResource("xmlsignature-config.xml");
        //Config config = new Config(configUrl.openStream());

        msLogger.debug("config path " + configUrl.getPath());
        msLogger.debug("config is created");
        return config;
    }

    public static tr.gov.tubitak.uekae.esya.api.signature.config.Config getSignatureConfig() throws IOException, ConfigurationException {

        msLogger.debug("getting config");
        InputStream configStream = getFile("esya-signature-config.xml");

        msLogger.debug("config is being created");

        tr.gov.tubitak.uekae.esya.api.signature.config.Config config = new tr.gov.tubitak.uekae.esya.api.signature.config.Config(configStream);

        URL configUrl = ResourceUtil.class.getResource("esya-signature-config.xml");
        //Config config = new Config(configUrl.openStream());

        msLogger.debug("config path " + configUrl.getPath());
        msLogger.debug("config is created");
        return config;
    }
	
	public static InputStream getLicense() throws Exception
	{
		return getFile(/*licensePath*/"lisans.xml");
	}
}
